import path from "path"
import fse from "fs-extra"
import { defineBuildConfig } from "unbuild"

const { BUILD_ENV } = process.env

export default defineBuildConfig(
  [
    {
      name: "web",

      outDir: "dist_web",

      entries: ["./src/web/index.ts"],

      rollup: {
        emitCJS: true,

        esbuild: {
          treeShaking: true,
          // minify: true,
          sourcemap: true,
        },

        // output: {
        //   assetFileNames: "env.duty.json",
        // },
      },

      alias: {
        "@": path.join(__dirname, "./src"),
      },

      // hooks: {
      //   "build:done": () => {
      //     fse.copySync("env.duty.json", "dist_web/web/env.duty.json")
      //   },
      // },

      failOnWarn: false,
    },
    {
      name: "main",

      outDir: "dist_main",

      entries: [
        "./src/index.ts",
        {
          input: "./src/scripts/",
          outDir: "./dist_main/scripts",
        },
      ],

      rollup: {
        emitCJS: true,

        esbuild: {
          treeShaking: true,
          minify: true,
          sourcemap: true,
        },
      },

      alias: {
        "@": path.join(__dirname, "./src"),
      },

      hooks: {
        "build:done": () => {
          fse.copySync("env.duty.json", "dist_main/env.duty.json")
        },
      },

      failOnWarn: false,
    },
  ].filter(i => (BUILD_ENV ? i.name == BUILD_ENV : true)),
)
